<?php
	
	class EncoderBehavior extends ModelBehavior {
		public function setup(&$model, $config = array()) {
			if (is_string($config)) {
				$config = array($config);
			}
			$this->settings[$model->alias] = $config;
		}

		public function encodeData(&$model, $data, $options = array()) {
			$_options = array(
				'json' => false,
				'trim' => true,
			);
			
			$options = array_merge($_options, $options);
	
			if (is_array($data) && count($data) > 0) {
				// trim
				if ($options['trim']) {
					$elements = array();
					foreach($data AS $id => $d) {
						$d = trim($d);
						if ($d != '') {
							$elements[$id] = '"' . $d . '"';
						}
					}
				} else {
					$elements = $data;
				}
	
				// encode
				if (count($elements) > 0) {
					if ($options['json']) {
						$output = json_encode($elements);
					} else {
						$output = '[' . implode(',', $elements) . ']';
					}
				} else {
					$output = '';
				}
			} else {
				$output = '';
			}
	
			return $output;
		}
	
		public function decodeData(&$model, $data) {
			if ($data == '') {
				$output = '';
			} else {
				$output = json_decode($data, true);
			}
			return $output;
		}
	}
